// miniprogram/pages/home/home.js
const appInstance = getApp()
const {
  barTitle,
} = appInstance.globalData

const bgam = wx.createInnerAudioContext('myAudio')
let wordsOfLoveTimer = null
const wordsOfLoveTimerDelay = 250

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    loading: true,
    id: '',
    paused: false,
    imgLoad: false,
    imgHeight: '924rpx'
  },

  async init(id) {
    this.endTypewriting()
    this.setData({
      loading: true,
      ['data.wordsOfLove2']: '',
    })
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
        name: 'remind',
        data: {
          action: 'getData',
          id
        },
        success: res => {
          // console.log('[云函数] [proxy] user openid: ', res.result)
          wx.setNavigationBarTitle({
            title: res.result.title,
          })
          if (!res.result.imgurl) {
            this.setData({
              imgLoad: true
            })
            this.startTypewriting()
          }
          this.setData({
            loading: false,
            data: res.result
          })
          resolve()
        },
        fail: reject
      })
    })
  },

  bindSuspension() {
    if (this.data.paused) {
      this.play()
    } else {
      this.paused()
    }
  },

  play() {
    bgam.play()
    this.setData({
      paused: false
    })
  },

  paused() {
    // console.log('paused', this.data.paused)
    bgam.pause()
    this.setData({
      paused: true
    })
  },

  startTypewriting() {
    let i = 0
    wordsOfLoveTimer = setInterval(() => {
      let {
        wordsOfLove2 = '',
          wordsOfLove = ''
      } = this.data.data
      this.setData({
        ['data.wordsOfLove2']: wordsOfLove2 += wordsOfLove[i]
      })
      if (i === wordsOfLove.length - 1) {
        i = 0
        this.endTypewriting()
      }
      i = i + 1
    }, wordsOfLoveTimerDelay)
  },

  endTypewriting() {
    clearInterval(wordsOfLoveTimer)
    wordsOfLoveTimer = null
  },

  bindload(e) {
    console.log('bindload', e);
    // this.setData({
    //   imgHeight: `${e.detail.height / 2}rpx`
    // })
    // 修复图片动画不连贯[加载完毕后有一定的延迟]
    setTimeout(() => {
      this.setData({
        imgLoad: true
      })
      this.startTypewriting()
    }, 400)
  },

  binderror() {
    // console.log('binderror');
    this.setData({
      imgLoad: true,
      imageLoadError: true
    })
  },

  loadBgam(url) {
    bgam.src = url
    bgam.loop = true
    bgam.onCanplay(() => {
      bgam.play()
      this.setData({
        paused: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      id
    } = options
    this.setData({
      id
    })
    this.init(id).then(() => {
      this.loadBgam(this.data.data && this.data.data.music && this.data.data.music.url)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.play()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.paused()
    this.endTypewriting()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.paused()
    this.endTypewriting()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      imgLoad: false
    })
    this.init(this.data.id).then(() => {
      this.setData({
        imgLoad: true
      })
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // const promise = new Promise(resolve => {
    //   setTimeout(() => {
    //     resolve({
    //       title: '自定义转发标题12212'
    //     })
    //   }, 2000)
    // })
    return {
      title: this.data.wordsOfLove,
      path: `/pages/remind/remind?id=${this.data.id}`,
      // promise
    }
  },
  onShareTimeline() {
    return {
      title: this.data.wordsOfLove,
      imageUrl: this.data.imgurl,
    }
  },
})